package mstest.wechat.service;

import mstest.wechat.base.config.ResponseCode;
import mstest.wechat.base.exp.BizException;
import mstest.wechat.bean.BindBean;
import mstest.wechat.dao.UserDAO;
import mstest.wechat.dao.UserRelaDAO;
import mstest.wechat.entity.User;
import mstest.wechat.entity.UserRela;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Transactional;

/**
 * Project Name: wechat
 * File Name: WechatService
 * Package Name: mstest.wechat.service
 * Date: 2018/8/7  11:27
 * Copyright (c) 2018, tianyul All Rights Reserved.
 */
@Service
public class WechatService {

    @Autowired
    private UserDAO userDAO;

    @Autowired
    private UserRelaDAO userRelaDAO;


    @Transactional(rollbackFor = {Exception.class}, isolation = Isolation.READ_COMMITTED)
    public UserRela save(String openid, String accessToken, Long expiresIn, String refreshToken) {
        //查询用户信息
        User param = new User();
        param.setOpenid(openid);

        User currentUser = userDAO.selectOne(param);
        if(null != currentUser) {
            //查询绑定信息
            UserRela relaParam = new UserRela();
            relaParam.setOpenid(openid);
            UserRela userRela = userRelaDAO.selectOne(relaParam);
            return null == userRela? relaParam: userRela;
        }

        User user = new User();
        user.setAccessToken(accessToken);
        user.setCreateTime(System.currentTimeMillis());
        user.setOpenid(openid);
        user.setExpireTime(System.currentTimeMillis() + (expiresIn - 600) * 1000);
        user.setRefreshToken(refreshToken);
        user.setRefreshExpireTime(System.currentTimeMillis() + 29 * 24 * 60 * 60 * 1000);

        userDAO.insert(user);

        //查询绑定信息
        UserRela relaParam = new UserRela();
        relaParam.setOpenid(openid);
        UserRela userRela = userRelaDAO.selectOne(relaParam);
        return null == userRela? relaParam: userRela;
    }

    /**
     * 获得用户信息
     * @param openid
     */
    public UserRela getUserInfo(String openid) {
        //查询用户信息
        User param = new User();
        param.setOpenid(openid);

        User user = userDAO.selectOne(param);
        if(null == user) {
            throw new BizException(ResponseCode.PARAM_ILLEGAL_ERROR, "用户不存在");
        }
        //查询绑定信息
        UserRela relaParam = new UserRela();
        relaParam.setOpenid(openid);
        UserRela userRela = userRelaDAO.selectOne(relaParam);
        return null == userRela? relaParam: userRela;
    }

    /**
     * 绑定信息
     * @param bindBean
     */
    @Transactional(rollbackFor = {Exception.class}, isolation = Isolation.READ_COMMITTED)
    public void bind(BindBean bindBean) {
        UserRela userRela = new UserRela();
        userRela.setOpenid(bindBean.getOpenid());
        userRela.setUserName(bindBean.getUserName());
        userRela.setCardNo(bindBean.getCardNo());

        userRelaDAO.insert(userRela);
    }


}
